package bytestream;

import com.mowwang.file.Buffered;

import java.io.*;

public class Copyest {
    public static void main(String[] args) {
            Long startTime=System.currentTimeMillis();
            re4();
            Long endTime=System.currentTimeMillis();
            System.out.println("耗时："+(endTime-startTime));
    }

    /**
     * 第1种：以单个字节来读
     * 加以复制
     * 先读入，在输出
     */
    public static void re1(){
        InputStream inputStream=null;
        OutputStream outputStream=null;
        try {
            inputStream=new FileInputStream("D:/banoffice/222.docx");
            outputStream=new FileOutputStream("E:/222.docx");
            int a=-1;
            while ((a=inputStream.read())!=-1)
            {
                outputStream.write(a);
            }
            outputStream.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {if (outputStream!=null)
            { outputStream.close();}

                if (inputStream!=null)
                { inputStream.close();}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    /**
     * 第2种以以字节数组方式来读
     */
    public static void re2(){
        InputStream inputStream=null;
        OutputStream outputStream=null;
        try {
            inputStream=new FileInputStream("D:/banoffice/222.docx");
            outputStream=new FileOutputStream("E:/222.docx");
            byte[] bytes=new byte[1024];
            int b=-1;
            while ((b=inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,b);
            }
            outputStream.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

                try { if (outputStream!=null)
                { outputStream.close();}

                    if (inputStream!=null)
                        inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }


        }

    }

    /**
     * 接下来用高效缓冲字节流来复制文件
     * 步骤与上面相同
     * 来看看毫秒数
     */
    public static void re3(){
        InputStream bis=null;
        OutputStream bos=null;
        try {
            bis=new BufferedInputStream(new FileInputStream("d:/movie/1.mp4"));
            bos=new BufferedOutputStream(new FileOutputStream("e:/1.mp4"));
            int c=-1;
            while ((c=bis.read())!=-1)
            {
                bos.write(c);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bos!=null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis!=null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void re4(){
        InputStream bis=null;
        OutputStream bos=null;
        try {
            bis=new BufferedInputStream(new FileInputStream("d:/movie/1.mp4"));
            bos=new BufferedOutputStream(new FileOutputStream("e:/1.mp4"));
            byte[] bytes=new byte[1024];
            int c=-1;
            while ((c=bis.read(bytes))!=-1)
            {
                bos.write(bytes,0,c);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bos!=null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis!=null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



}
